#include <QtGui>
#include "mainwindow.h"

#include "controlpanel.h"

#include "grid.h"
#include "CPDisplay.h"
#include "glwidget.h"

MainWindow::MainWindow() : QWidget()
{
    Grid *mothership = new Grid(200, 200, 400, 200, 20);
    Grid *pdSquare = new Grid(375, 500, 50, 50, 25);

    ControlPanel *controlPanel = new ControlPanel(this, mothership);
    CPDisplay *cpDisplay = new CPDisplay(this, mothership, pdSquare);

    QHBoxLayout *layout = new QHBoxLayout;
    layout->addWidget(cpDisplay);
    layout->addWidget(controlPanel);
    layout->setSpacing(0);
    setLayout(layout);

    setWindowTitle(tr("Projector Control Panel"));

    GLWidget *projectorDisplay = new GLWidget(this, mothership, controlPanel, pdSquare);
    projectorDisplay->show();

    ///////////////

    connect(controlPanel->ui.prestimFrames, SIGNAL(editingFinished()), controlPanel, SLOT(framesChanged()));
    connect(controlPanel->ui.stimFrames, SIGNAL(editingFinished()), controlPanel, SLOT(framesChanged()));
    connect(controlPanel->ui.poststimFrames, SIGNAL(editingFinished()), controlPanel, SLOT(framesChanged()));
    connect(controlPanel->ui.nStimuli, SIGNAL(editingFinished()), controlPanel, SLOT(framesChanged()));
    connect(controlPanel->ui.framerate, SIGNAL(editingFinished()), controlPanel, SLOT(framesChanged()));

    connect(controlPanel->ui.gridWidth, SIGNAL(editingFinished()), controlPanel, SLOT(gridWidthChanged()));
    connect(controlPanel->ui.gridWidth, SIGNAL(editingFinished()), cpDisplay->qgrid, SLOT(update()));
    connect(controlPanel->ui.gridWidth, SIGNAL(editingFinished()), controlPanel, SLOT(gridDimsChanged()));

    connect(controlPanel->ui.nStimuli, SIGNAL(editingFinished()), controlPanel, SLOT(coverageChanged()));
    connect(controlPanel->ui.nSpots, SIGNAL(editingFinished()), controlPanel, SLOT(coverageChanged()));

    connect(controlPanel->ui.runBtn, SIGNAL(clicked()), projectorDisplay, SLOT(runStimuli()));
    connect(controlPanel->ui.randBtn, SIGNAL(clicked()), cpDisplay->qgrid, SLOT(clear()));
    connect(controlPanel->ui.randBtn, SIGNAL(clicked()), projectorDisplay, SLOT(runRandomSpots()));
    connect(controlPanel->ui.sequencerBtn, SIGNAL(clicked()), cpDisplay->qgrid, SLOT(Clear()));
    connect(controlPanel->ui.sequencerBtn, SIGNAL(clicked()), projectorDisplay, SLOT(runSequence()));

    connect(controlPanel->ui.highlightEdgesBtn, SIGNAL(toggled(bool)), cpDisplay->qgrid, SLOT(highlightEdges(bool))); // need to reorganize classes?
    connect(controlPanel->ui.highlightEdgesBtn, SIGNAL(toggled(bool)), cpDisplay->qpdSquare, SLOT(highlightEdges(bool))); // need to reorganize classes?
    connect(controlPanel->ui.highlightEdgesBtn, SIGNAL(toggled(bool)), projectorDisplay, SLOT(updateGL()));

    connect(controlPanel->ui.clearBtn, SIGNAL(clicked()), cpDisplay->qgrid, SLOT(clear()));
    connect(controlPanel->ui.clearBtn, SIGNAL(clicked()), cpDisplay->qpdSquare, SLOT(clear()));

    connect(controlPanel->ui.displayNum, SIGNAL(currentIndexChanged(int)), projectorDisplay, SLOT(displayNumChanged(int)));

    connect(controlPanel->ui.resolution, SIGNAL(currentIndexChanged(QString)), projectorDisplay, SLOT(resolutionChanged(QString)));

    connect(controlPanel->ui.triggerLine, SIGNAL(editingFinished()), projectorDisplay, SLOT(triggerLineChanged()));

    connect(controlPanel->ui.savePath, SIGNAL(editingFinished()), projectorDisplay, SLOT(savePathChanged()));

    //connect(controlPanel->ui.loadBtn, SIGNAL(clicked()), controlPanel, SLOT(loadButtonPushed()));

    connect(cpDisplay->qgrid, SIGNAL(gridMove()), projectorDisplay, SLOT(updateGL()));
    connect(cpDisplay->qpdSquare, SIGNAL(gridMove()), projectorDisplay, SLOT(updateGL()));
    connect(cpDisplay->qgrid, SIGNAL(gridMove()), controlPanel, SLOT(gridDimsChanged()));
}
